<?php 
if (!defined("LEVEL") || LEVEL < 30 || !defined("OK")) { header('location: ?home'); }
?>
<script language="JavaScript">
// Notice: The simple theme does not use all options some of them are limited to the advanced theme
tinyMCE.init({
	mode : "textareas",
	theme : "advanced",
	mode : "exact",
	plugins : "style,layer,table,advhr,emotions,advimage,advlink,insertdatetime,preview,media,searchreplace,contextmenu,directionality,fullscreen,noneditable,visualchars,xhtmlxtras",
	elements : "naujiena,placiau",
	content_css : "stilius/stilius.css",
	//theme_advanced_resize_horizontal : false,
	theme_advanced_resizing : true,
	apply_source_formatting : true,
	theme_advanced_path_location : "bottom",
	theme_advanced_buttons1_add_before : "save,newdocument,separator",
	theme_advanced_buttons1_add : "fontsizeselect",
	theme_advanced_buttons2_add : "separator,insertdate,inserttime,preview,|,forecolor,backcolor",
	theme_advanced_buttons2_add_before: "search,replace,|",
	theme_advanced_buttons3_add_before : "tablecontrols,|",
	theme_advanced_buttons3_add : "emotions,media,advhr,|,fullscreen",
	theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops",
	theme_advanced_disable : "help"
});

</script>
<?php
unset($extra);
if (isset($_POST['action']) && $_POST['action'] == 'Trinti' && LEVEL == 30 && isset($_POST['edit_new'])) {
	mysql_query1("DELETE FROM `naujienos` WHERE id=".escape($_POST['edit_new']) ." LIMIT 1");
	if (mysql_affected_rows() > 0) { msg("Informacija","Naujiena sėkmingai ištrinta"); }
	else { klaida("Klaida","Naujienos trinimo klaida"); }
	mysql_query1("DELETE FROM `kom` WHERE pid=50 AND kid=".escape($_POST['edit_new']) ."");
	redirect("?id,46;a,2","meta");
}
if (isset($_POST['edit_new']) && isnum($_POST['edit_new']) && $_POST['edit_new'] > 0) {
	$extra = mysql_query1("SELECT `pavadinimas`, `naujiena`, `daugiau`, `kom` FROM `naujienos` WHERE `id`=".escape($_POST['edit_new'])." LIMIT 1");
	$extra = mysql_fetch_assoc($extra);
}
if (isset($_POST['action']) && $_POST['action'] == 'update_new') {
			mysql_query1("UPDATE `naujienos` SET
			`pavadinimas` = ".escape($_POST['pav']).",
			`naujiena` = ".escape($_POST['naujiena']."<br />Redagavo: ".$_SESSION['username']."").",
			`daugiau` = ".escape($_POST['placiau'])."
			WHERE `id`=".escape($_POST['news_id']).";
			") or klaida("Klaida",mysql_error());
}
if (isset($_POST['action']) && $_POST['action'] == 'add_new') {
	$naujiena = $_POST['naujiena'];
	$placiau = $_POST['placiau'];
	$komentaras = input($_POST['kom']);
	$pavadinimas = input(htmlspecialchars($_POST['pav']));
	if (empty($naujiena) || empty($pavadinimas)) { $error = "Nepilnai užpildyti laukeliai"; }
	if (!$error) {
		$result = mysql_query1("INSERT INTO naujienos (pavadinimas, naujiena, daugiau, data, autorius, kom)
		VALUES (".escape($pavadinimas).", ".escape($naujiena).", ".escape($placiau).",  '" . date("Y-m-d H:i:s") ."', ".escape($_SESSION['username']).", ".escape($komentaras).")");
		if ($result) { msg("Informacija","Naujiena sėkmingai patalpinta"); }
		else { klaida("Klaida","Naujiena nepatalpinta. Klaida:<br><b>" . mysql_error() ."</b>"); }
	}
	else { klaida("Klaida",$error); }
}
unset($naujiena,$placiau,$komentaras,$pavadinimas,$result,$error);
?>
<h2>Naujienų administravimas</h2>
<?php
$sql = mysql_query1("SELECT id,pavadinimas FROM  naujienos ORDER BY ID DESC");
?>
<fieldset>
<legend>Naujienos redagavimas</legend>
<form name="form1" action="?id,46;a,2" method="post">
<table border=0 width=100%>
	<tr>
		<td width=10%>Naujiena:</td>
		<td>
			<select size="1" name="edit_new">
				<?php
				while ($row = mysql_fetch_assoc($sql)) {
					echo "<option value=" . $row['id'] .">" . $row['pavadinimas'] ."</option>\n";
				}
				?>
			</select>
			<input type="submit" value="Redaguoti">
			<input type="submit" name="action" value="Trinti">
		</td>
	</tr>
</table>
</form>
</fieldset>
<br>
<fieldset>
<legend>Įkelti naujieną</legend>
<form name="add_new" action="?id,46;a,2" method="post">
<table width=100% border=0>
	<tr>
		<td width=15%>Pavadinimas</td>
		<td><input name="pav" type="text" value="<?=(isset($extra))?$extra['pavadinimas']:''?>" size="100"></td>
	</tr>
	<tr>
		<td>Leisti komentarus:</td>
		<td>
			<select size="1" name="kom" <?=(isset($extra))?'disabled':''?>>
				<option value="taip" selected>Taip</option>
				<option value="ne">Ne</option>
			</select>
		</td>
	</tr>
	<tr>
		<td valign='top' align='left'>Naujiena:</td>
		<td><textarea name="naujiena" rows="20" wrap="on" style="width:100%"><?=(isset($extra))?input($extra['naujiena']):''?></textarea></td>
	</tr>
	<tr>
		<td valign='top' align='left'>Plačiau:</td>
		<td><textarea name="placiau" rows="20" wrap="on" style="width:100%"><?=(isset($extra))?input($extra['daugiau']):''?></textarea></td>
	</tr>
</table>
<input type="submit" value="<?=(isset($extra))?'Redaguoti':'Pateikti'?>">
<input type="hidden" name="action"  value="<?=(isset($extra))?'update_new':'add_new'?>" />
<? if (isset($extra)) { ?> <input type="hidden" name="news_id" value="<?=$_POST['edit_new']?>"> <? } ?>
</form>
<?php unset($sql,$extra,$row); ?>